﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace BoxBinary.AspectCache.Core.Helpers
{
    /// <summary>
    /// Contains strongly-typed methods for retrieving values from the QueryString and Form collections
    /// </summary>
    public class PostParams
    {
        /// <summary>
        /// Returns a value from the HttpRequest.Params collection
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <returns>object if the Key is found, or null if it is not</returns>
        public static object GetValue(string key, object defaultValue)
        {
            return GetValue(key, defaultValue, HttpContext.Current.Request.Params);
        }

        /// <summary>
        /// Returns a value from a NameValueCollection
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <param name="collection"><see cref="NameValueCollection" /> containing post parameters</param>
        /// <returns>object if the Key is found, or null if it is not</returns>
        public static object GetValue(string key, object defaultValue, NameValueCollection collection)
        {
            object o = collection[key];
            if (o == null)
                o = defaultValue;
            return o;
        }


        /// <summary>
        /// Returns a value from the HttpRequest.Params collection strongly typed to a string
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <returns>string value if the Key is found, or defaultValue if it is not</returns>
        public static string GetString(string key, string defaultValue)
        {
            object o = GetValue(key, defaultValue).ToString();
            if (o == null)
                return defaultValue;
            else
                return o.ToString();
        }

        public static List<String> GetStrings(string key)
        {
            return GetStrings(key, HttpContext.Current.Request.Params);
        }

        public static List<String> GetStrings(string key, NameValueCollection collection)
        {
            if (collection[key] == null)
                return new List<string>();
            else
            {
                return collection[key].Split(',').ToList();
            }
        }

        /// <summary>
        /// Returns a value from a NameValueCollection strongly typed to a string
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <param name="collection"><see cref="NameValueCollection" /> containing post parameters</param>
        /// <returns>string value if the Key is found, or defaultValue if it is not</returns>
        public static string GetString(string key, string defaultValue, NameValueCollection collection)
        {
            object o = GetValue(key, defaultValue, collection).ToString();
            if (o == null)
                return defaultValue;
            else
                return o.ToString();
        }

        /// <summary>
        /// Returns a value from the HttpRequest.Params collection strongly typed to an Int32
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <returns>Int32 value if the Key is found, or defaultValue if it is not</returns>
        public static Int32 GetInt32(string key, Int32 defaultValue)
        {
            Int32 result = defaultValue;
            Int32.TryParse(GetString(key, defaultValue.ToString()), out result);
            return result;
        }

        /// <summary>
        /// Returns a value from a NameValueCollection strongly typed to an Int32
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <param name="collection"><see cref="NameValueCollection" /> containing post parameters</param>
        /// <returns>Int32 value if the Key is found, or defaultValue if it is not</returns>
        public static Int32 GetInt32(string key, Int32 defaultValue, NameValueCollection collection)
        {
            Int32 result = defaultValue;
            Int32.TryParse(GetString(key, defaultValue.ToString(), collection), out result);
            return result;
        }

        /// <summary>
        /// Returns a value from the HttpRequest.Params collection strongly typed to an Int64
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <returns>Int64 value if the Key is found, or defaultValue if it is not</returns>
        public static Int64 GetInt64(string key, Int64 defaultValue)
        {
            Int64 result = defaultValue;
            Int64.TryParse(GetString(key, defaultValue.ToString()), out result);
            return result;
        }

        /// <summary>
        /// Returns a value from a NameValueCollection strongly typed to an Int64
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <param name="collection"><see cref="NameValueCollection" /> containing post parameters</param>
        /// <returns>Int64 value if the Key is found, or defaultValue if it is not</returns>
        public static Int64 GetInt64(string key, Int64 defaultValue, NameValueCollection collection)
        {
            Int64 result = defaultValue;
            Int64.TryParse(GetString(key, defaultValue.ToString(), collection), out result);
            return result;
        }

        /// <summary>
        /// Returns a value from the HttpRequest.Params collection strongly typed to an Int32
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <returns>Boolean value if the Key is found, or defaultValue if it is not</returns>
        public static bool GetBoolean(string key, bool defaultValue)
        {
            bool result = defaultValue;
            bool.TryParse(GetString(key, defaultValue.ToString()), out result);
            return result;
        }

        /// <summary>
        /// Returns a value from a NameValueCollection strongly typed to an Int32
        /// </summary>
        /// <param name="key">Key to lookup the value from the collection</param>
        /// <param name="defaultValue">Default value to be returned if the collection does not contain the Key</param>
        /// <param name="collection"><see cref="NameValueCollection" /> containing post parameters</param>
        /// <returns>Boolean value if the Key is found, or defaultValue if it is not</returns>
        public static bool GetBoolean(string key, bool defaultValue, NameValueCollection collection)
        {
            bool result = defaultValue;
            bool.TryParse(GetString(key, defaultValue.ToString(), collection), out result);
            return result;
        }
    }
}